package wqh.controller.json;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import wqh.domain.Device;
import wqh.global.GlobalDevice;
import wqh.pojo.DeviceJsonPojo;
import wqh.pojo.IPConver;
import wqh.pojo.MapRet;
import wqh.service.DeviceService;
import wqh.service.SystemLogService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by devts on 2016/9/25.
 */
@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/xkDevice")
public class DeviceJSON {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private DeviceService deviceService;

    @Resource
    private SystemLogService systemLogService;

    @ResponseBody
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public Object getAll()
    {

        List<Device> deviceList = this.deviceService.findAllRecord();
        List<DeviceJsonPojo> deviceJsonPojoList = new ArrayList<DeviceJsonPojo>();
        for(Device device : deviceList)
        {
            if(GlobalDevice.exist(IPConver.int2Ip(device.getIp())))
            {
                device.setStatus((byte)1);
            }
            else
            {
                device.setStatus((byte)0);
            }
            deviceJsonPojoList.add(new DeviceJsonPojo(device));
        }
        return MapRet.DATA(deviceJsonPojoList);
    }

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object add(@RequestBody DeviceJsonPojo deviceIn)
    {
        try {
            List<Device> deviceOldList = deviceService.findByIp(IPConver.Ip2Int(deviceIn.getIp()));
            if(deviceOldList.size() != 0) return MapRet.SQL_ERROR("IP已存在");
            Device device = new Device();
            device.setName(deviceIn.getName());
            device.setIp(IPConver.Ip2Int(deviceIn.getIp()));
            device.setStatus((byte) 0);
            device.setDescription(deviceIn.getDescription());
            device.setPhone(deviceIn.getPhone());
            deviceService.insertRecord(device);
        }catch (Exception e)
        {
            e.printStackTrace();
            logger.error("添加设备出错：" + e.toString());
            return MapRet.SQL_ERROR();
        }
        return MapRet.SQL_SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(@RequestBody DeviceJsonPojo deviceIn)
    {
        String originName = deviceService.findNameById(deviceIn.getId());
        System.out.println(deviceIn.toString());
        try {
            Device device = new Device();
            device.setId(deviceIn.getId());
            device.setName(deviceIn.getName());
            device.setIp(IPConver.Ip2Int(deviceIn.getIp()));
            device.setStatus((byte) 0);
            device.setPhone(deviceIn.getPhone());
            device.setDescription(deviceIn.getDescription());
            deviceService.updateRecord(device);
        }catch (Exception e)
        {
            e.printStackTrace();
            logger.error("修改设备出错 " + e.toString());
            return MapRet.SQL_ERROR();
        }
        return MapRet.SQL_SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object delete(@RequestParam(value = "id",required = true) int id)
    {
        String originName = null;
        String originIp = null;
        try {
            Device device = deviceService.findAllByID(id);
            originName = device.getName();
            originIp = IPConver.int2Ip(device.getIp());
            deviceService.deleteRecord(id);

        }catch (Exception e)
        {
            e.printStackTrace();
            logger.error("删除设备出错 " + e.toString());
            return MapRet.SQL_ERROR();
        }

        return MapRet.SQL_SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public Object get(@RequestParam(value = "id",required = true) int id)
    {
        Device device = deviceService.findAllByID(id);
        if(device == null)
        {
            return MapRet.SQL_SELECT_ERROR();
        }
        return MapRet.DATA(new DeviceJsonPojo(device));
    }


}
